{% extends "index.html" %}
{% block title %}
    图书列表
{% endblock %}
{% block nav-list %}
    <a href="/" class="list-group-item list-group-item-action">首页</a>
    <a href="/book/list" class="list-group-item list-group-item-action  border-info bg-info text-white">图书列表</a>
    <a href="/author/list" class="list-group-item list-group-item-action">作者列表</a>
    <a href="/publish/list" class="list-group-item list-group-item-action">出版社列表</a>
    <a class="list-group-item list-group-item-action disabled">更多</a>
{% endblock %}
{% block content-title %}
    图书列表
{% endblock %}
{% block content %}
    <table class="table table-striped table-hover">
        <thead>
        <td>ID</td>
        <td>书名</td>
        <td>价格</td>
        <td>出版日期</td>
        <td>出版社</td>
        <td>作者</td>
        <td>操作</td>
        </thead>
        <tbody>
        {% for book in book_queryset %}
            <tr>
                <td>{{ book.pk }}</td>
                <td>{{ book.title }}</td>
                <td>{{ book.price }}</td>
                <td>{{ book.publish_date | date:"Y-m-d" }}</td>
                <td>{{ book.publish.name }}</td>
                <td>
                    {% for author in book.authors.all %}
                        {{ author.name }}
                    {% endfor %}

                </td>
                <td>
                    <a class="btn btn-info" href="/book/add">添加</a>
                    <a class=" btn btn-secondary" href="/book/modify/?book_id={{ book.pk }}">修改</a>
                    <a class="btn btn-danger" delete_id="{{ book.pk }}">删除</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
{% block js %}
    <script>

        $('.btn-danger').click(function () {
            let currentBtn = $(this)
            //询问框

            layer.confirm('是否删除？', {
                btn: ['确定', '取消'] //按钮
            }, function () {

                $.ajax({
                    url: '/book/delete/',
                    type: 'post',
                    data: {book_id: currentBtn.attr('delete_id')},
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.msg, {icon: 1});
                            location.replace('/book/list')
                        }
                    }
                })
            });


        })
    </script>
{% endblock %}